package cn.maidouya.hrm.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
 *   security认证配置
 */
@Configuration // 告诉spring这个配置文件并交给spring管理
@EnableWebSecurity // 开启security支持
public class HrmsecurityAuthenConfig extends WebSecurityConfigurerAdapter {
    // 1. 解密器
    @Bean
    public PasswordEncoder getEncoder(){
        return new BCryptPasswordEncoder();
    }

    // 2. security安全配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests() // 所有的请求都要认证
               // .antMatchers("/login").permitAll() // 登录页面路径放行
                .antMatchers("/loginUser/login").permitAll() // 登录页面路径放行
                //.antMatchers("/courseDoc/queryCourses").permitAll() // 登录页面路径放行
                .anyRequest().authenticated() // 所有请求都要认证
                .and().formLogin() // 支持表单登录
                .successForwardUrl("/success") // 登录成功回调地址可以映射到controller层
                .and().csrf().disable(); // 关闭跨站
    }

    // 配置认证管理器，授权模式为“poassword”时会用到
    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}